package com.four.peoplemanage.overtime.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.overtime.dao.OverDao;
import com.four.peoplemanage.overtime.entity.Overtime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OvertimeService {
    @Autowired
    OverDao overDao;
    //申请加班
    public boolean insert(Overtime overtime){
        if (overtime == null){
            return false;
        }
        overtime.setStatus("待审核");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataTimeString = sdf.format(new Date());
        overtime.setOverbegintime(dataTimeString);
        overtime.setOverendtime(dataTimeString);
        return overDao.addOvertime(overtime)>0;
    }

    //分页
    public Page<Overtime> getPageOvertime (int pageNum, int pageSize, String name, int information_id,String overtime_type,  String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Overtime> list=overDao.getOvertime(start,pageSize,name,overtime_type,information_id,status);
        //获取总条目数
        int total=overDao.countOvertime(name,overtime_type,status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Overtime> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //分页
    public Page<Overtime> getAllOvertime (int pageNum, int pageSize, String name, String overtime_type,  String status){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Overtime> list=overDao.getAllOvertime(start,pageSize,name,overtime_type,status);
        //获取总条目数
        int total=overDao.countOvertime(name,overtime_type,status);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Overtime> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //撤销
    public  boolean updateStatus(Overtime overtime){
        if (overtime==null){
            return false;
        }
        overtime.setStatus("已撤销");
        return overDao.updateOvertime(overtime)>0;
    }

    //审核通过
    public  boolean updateSuccess(Overtime overtime){
        if (overtime==null){
            return false;
        }
        overtime.setStatus("已通过");
        return overDao.updateOvertime(overtime)>0;
    }

    //审核驳回
    public  boolean updateFail(Overtime overtime){
        if (overtime==null){
            return false;
        }
        overtime.setStatus("已驳回");
        return overDao.updateOvertime(overtime)>0;
    }

    //编辑
    public boolean updateOvertime(Overtime overtime){
        if (overtime == null){
            return false;
        }
        overtime.setStatus("待审核");
        return overDao.updateOvertimeById(overtime)>0;
    }
}
